﻿Imports AForge.Imaging.Filters
Imports AForge
Imports UCBase
Imports System.Drawing

Public Class ColorReplace
    Implements I_ImageExchange
    Implements I_LoadSave
    Private WithEvents SourceEvent As I_ImageExchange
    Private Backpic As New BackgroundCreator
    Private Mov As New Mover
    Private SourceBitmap As System.Drawing.Bitmap
    Private OverLay As Bitmap
    Private DestBitmap As System.Drawing.Bitmap

    Private ByPassed As Boolean = False

    Public Event ImageReady(ByVal Image As System.Drawing.Bitmap) Implements I_ImageExchange.ImageOut1

    Private RedBuff As Byte()
    Private GreenBuff As Byte()
    Private BlueBuff As Byte()

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.BackgroundImage = Backpic.CreatePic(Me.Width, Me.Height, Color.CadetBlue)
        Mov.Connect(Me)
        DrawWave()
    End Sub

    Private Sub SourceEvent_ImageReady(ByVal Image As System.Drawing.Bitmap) Handles SourceEvent.ImageOut1
        SourceBitmap = Image
        If ByPassed Then
            DestBitmap = SourceBitmap
            RaiseEvent ImageReady(DestBitmap)
        Else
            ProcFilter()
        End If
    End Sub
    Private Sub ProcFilter()
        Dim Tmp As Bitmap
        Dim filterIn As Grayscale = New Grayscale(0.2125, 0.7154, 0.0721)
        Tmp = filterIn.Apply(SourceBitmap)

        Dim TmpB As Bitmap
        Dim filterInB As GrayscaleToRGB = New GrayscaleToRGB
        TmpB = filterInB.Apply(Tmp)
        If CheckBox1.Checked = False Then
            Dim filter As ColorRemapping = New ColorRemapping(RedBuff, GreenBuff, BlueBuff)
            DestBitmap = filter.Apply(TmpB)
            Tmp.Dispose()
            TmpB.Dispose()
        Else
            DestBitmap = TmpB
        End If

        RaiseEvent ImageReady(DestBitmap)
    End Sub

    Private Sub SocketIn_Connect() Handles SocketIn.Connect
        Dim S As Socket = SocketIn.Remote
        SourceEvent = S.Parent
    End Sub

    Private Sub BlankTransformFilter_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        '677, 497
        Me.Width = 675
        Me.Height = 500
    End Sub

    Private Sub Button3D5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3D5.Click
        If ByPassed = False Then
            ByPassed = True
            EnableLed.State = Led.LedState.Led_On
        Else
            ByPassed = False
            EnableLed.State = Led.LedState.Led_Off
        End If
    End Sub
    Public Function GetSettings() As String Implements UCBase.I_LoadSave.GetSettings
        Return ""
    End Function

    Public Sub SetSettings(ByVal Settings As String) Implements UCBase.I_LoadSave.SetSettings

    End Sub

    Private Sub DrawWave()
        Dim BMP As New Bitmap(510, 285, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        Dim G As Graphics = Graphics.FromImage(BMP)

        RedBuff = CreateBuffer(TrackBar2.Value, TrackBar1.Value, TrackBar3.Value)
        GreenBuff = CreateBuffer(TrackBar5.Value, TrackBar6.Value, TrackBar4.Value)
        BlueBuff = CreateBuffer(TrackBar8.Value, TrackBar9.Value, TrackBar7.Value)

        Dim x1 As Integer
        Dim x2 As Integer
        Dim y1 As Integer
        Dim y2 As Integer

        For c = 1 To 255
            x1 = (c - 1) * 2
            x2 = c * 2
            y1 = 265 - RedBuff(c - 1)
            y2 = 265 - RedBuff(c)
            Dim RP As New Pen(Color.Red, 2)
            G.DrawLine(RP, x1, y1, x2, y2)

            y1 = 265 - GreenBuff(c - 1)
            y2 = 265 - GreenBuff(c)
            Dim GP As New Pen(Color.Green, 2)
            G.DrawLine(GP, x1, y1, x2, y2)

            y1 = 265 - BlueBuff(c - 1)
            y2 = 265 - BlueBuff(c)
            Dim BP As New Pen(Color.Blue, 2)
            G.DrawLine(BP, x1, y1, x2, y2)

            Dim P As New Pen(Color.FromArgb(RedBuff(c), GreenBuff(c), BlueBuff(c)), 2)
            x1 = c * 2
            x2 = x1
            y1 = 266
            y2 = 285
            G.DrawLine(P, x1, y1, x2, y2)

            PictureBox1.Image = BMP
        Next
    End Sub
    Private Function CreateBuffer(ByVal Offset As Integer, ByVal Span As Integer, ByVal Level As Integer) As Byte()
        Dim Buff(255) As Byte
        Dim D As Double
        Dim V As Integer

        For c = 0 To 255
            Buff(c) = 0
        Next
        For c = 0 To Span
            D = (c * (180 / Span)) * (System.Math.PI / 180)
            If Level > 0 Then
                V = Level * System.Math.Sin(D)
            Else
                V = 0
            End If
            If V > 255 Then V = 255
            If V < 0 Then V = 0
            If (c + Offset) >= 0 And (c + Offset) <= 255 Then
                Buff(c + Offset) = V
            End If
        Next
        Return Buff
    End Function
    Private Sub TrackBar5_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar5.Scroll
        DrawWave()
    End Sub

    Private Sub TrackBar6_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar6.Scroll
        DrawWave()
    End Sub

    Private Sub TrackBar4_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar4.Scroll
        DrawWave()
    End Sub

    Private Sub TrackBar8_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar8.Scroll
        DrawWave()
    End Sub

    Private Sub TrackBar9_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar9.Scroll
        DrawWave()
    End Sub

    Private Sub TrackBar7_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar7.Scroll
        DrawWave()
    End Sub
    Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
        DrawWave()
    End Sub

    Private Sub TrackBar3_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar3.Scroll
        DrawWave()
    End Sub
    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        DrawWave()
    End Sub
End Class
